<?php   
		include('../data/common.inc.php');
        $charset = $cfg_db_language; // 网站编码 ( 示例：UTF-8、GBK )
		$weblink = $_SERVER['SERVER_NAME']; // 域名( 示例：apple.com )
		$http= $_SERVER['HTTP_HOST'];
		$https = explode('/',$_SERVER['REQUEST_URI']);
		require('city.php');
        $Py = unserialize(file_get_contents('http://'.$weblink.'/mycity/pinyin.txt'));
        $con = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
		if(!$con){die;}
		mysql_select_db($cfg_dbname, $con);
		$result = mysql_query('SELECT DISTINCT defaultname FROM '.$cfg_dbprefix.'arctype');
		while($row = mysql_fetch_array($result)){
			if($row['defaultname']!='index.html'){
				$default.= $row['defaultname'].'@';
			}
		}
		$default = substr($default, 0,-1);
		$edefault = explode('@', $default);
			foreach ($city as $ki => $vi) {
		    	$str = str_replace(" ","",$vi['province_name']);
		        $str_arr = utf8_str_split($str);
		        $result = array('');
	    	    foreach($str_arr as $char){
		            if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$char)){
		                $pinyin = $Py[$char];
		                $temp_result = array();
		                foreach($pinyin as $py){
		                    foreach($result as $v){
		                        $temp_result[] = $v.$py.'';
		                    }
		                }
		                $result = $temp_result;
		            }else{
		                foreach($result as $k=>$v){
		                    $result[$k] .= $char;
		                }
		            }
		        }
			  	$city[$ki]['province_name'] = '_'.$result['0'].'_'.$vi['province_name'];
			}

			foreach ($city as $ki => $vi) {
				foreach ($vi['city'] as $a => $b) {
			    	$str = str_replace(" ","",$b['city_name']);
			        $str_arr = utf8_str_split($str);
			        $result = array('');
		    	    foreach($str_arr as $char){
			            if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$char)){
			                $pinyin = $Py[$char];
			                $temp_result = array();
			                foreach($pinyin as $py){
			                    foreach($result as $v){
			                        $temp_result[] = $v.$py.'';
			                    }
			                }
			                $result = $temp_result;
			            }else{
			                foreach($result as $k=>$v){
			                    $result[$k] .= $char;
			                }
			            }
			        }
			        $city[$ki]['city'][$a]['city_name'] = '_'.$result['0'].'_'.$b['city_name'];
				}
			}
			foreach ($city as $ki => $vi) {
				foreach ($vi['city'] as $a => $b) {
					if(isset($b['area'])){
						foreach ($b['area'] as $c => $d) {
							$str = str_replace(" ","",$d);
					        $str_arr = utf8_str_split($str);
					        $result = array('');
				    	    foreach($str_arr as $char){
					            if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$char)){
					                $pinyin = $Py[$char];
					                $temp_result = array();
					                foreach($pinyin as $py){
					                    foreach($result as $v){
					                        $temp_result[] = $v.$py.'';
					                    }
					                }
					                $result = $temp_result;
					            }else{
					                foreach($result as $k=>$v){
					                    $result[$k] .= $char;
					                }
					            }
					        }
						  	$city[$ki]['city'][$a]['area'][$c] = '_'.$result['0'].'_'.$d;
						}
					}
				}
			}

		foreach ($edefault as $k => $v) {if($v==$https[1] OR empty($https[1])){$is=1;}}
		if($is==1){
			foreach ($city as $k => $v) {
				  	$provincename = mb_convert_encoding($v['province_name'], $charset, 'utf8');
			        $province_name = explode('_', $provincename);
	                $L[]='<a href="http://'.$weblink.'/'.$province_name['1'].'/index.html'.'">'.$province_name['2'].'</a>';
		    }
		}else{
			foreach ($city as $k => $v) {
				if(strstr($v['province_name'],'_'.$https[1].'_')){
					$exh = explode('_', $v['province_name']);
					foreach ($v['city'] as $ke => $va) {
                        $city_name = mb_convert_encoding($va['city_name'], $charset, 'utf8');
						$cityname = explode('_', $city_name);
						$L[]='<a href="http://'.$weblink.'/'.$cityname['1'].'/index.html'.'">'.$cityname['2'].'</a>';
					}
					$web = mb_convert_encoding($exh['2'], $charset, 'utf8');
				}
			}
			if(!$web){
				foreach ($city as $k => $v) {
					foreach ($v['city'] as $ke => $va) {
						if(strstr($va['city_name'],'_'.$https[1].'_')){
							$exh = explode('_', $va['city_name']);
							foreach ($v['city'] as $ke => $va) {
		                        $city_name = mb_convert_encoding($va['city_name'], $charset, 'utf8');
								$cityname = explode('_', $city_name);
								$L[]='<a href="http://'.$weblink.'/'.$cityname['1'].'/index.html'.'">'.$cityname['2'].'</a>';
							}
							$web = mb_convert_encoding($exh['2'], $charset, 'utf8');
						}
					}
				}
			}

			// if(!$web){
			// 	foreach ($city as $k => $v) {
			// 		foreach ($v['city'] as $ke => $va) {
			// 			if(strstr($va['city_name'],'_'.$https[1].'_')){
			// 				$exh = explode('_', $va['city_name']);
			// 				foreach ($va['area'] as $key => $val) {
			// 					$area = mb_convert_encoding($val, $charset, 'utf8');
			// 					$areas = explode('_', $area);
			// 					$L[]='<a href="http://'.$weblink.'/'.$areas['1'].'/index.html'.'">'.$areas['2'].'</a>';
			// 				}
			// 				$web = mb_convert_encoding($exh['2'], $charset, 'utf8');
			// 			}
			// 		}
			// 	}
			// }
			
			// if(!$web){
			// 	foreach ($city as $k => $v) {
			// 		foreach ($v['city'] as $hke => $hva) {
			// 			foreach ($v['city'] as $ke => $va) {
			// 				foreach ($va['area'] as $key => $val) {
			// 					if(strstr($val,'_'.$ex['0'].'_')){
			// 						$exh = explode('_', $val);
			// 						$web = mb_convert_encoding($exh['2'], $charset, 'utf8');
			// 					}
			// 				}
			// 			}
			// 		}
			// 	}
			// }
		}


		foreach ($city as $k => $v) {
			$provincename = mb_convert_encoding($v['province_name'], $charset, 'utf8');
	        $province_name = explode('_', $provincename);
            $A[]='<dt><a href="http://'.$weblink.'/'.$province_name['1'].'/index.html'.'">'.$province_name['2'].'</a></dt><dd>';
			foreach ($v['city'] as $ke => $va) {
                        $city_name = mb_convert_encoding($va['city_name'], $charset, 'utf8');
						$cityname = explode('_', $city_name);
						$A[]='<a href="http://'.$weblink.'/'.$cityname['1'].'/index.html'.'">'.$cityname['2'].'</a>';
			}
			$A[] = '</dd>';
		}
		if($web){
			if($https[1]=='index.html'){
				define("cityurl",'');
			}else{
				foreach ($edefault as $key => $val) {
					if($https[1]==$val){
						define("cityurl",'');
					}
				}
				if(!defined(cityurl)){
					define("cityurl",'/'.$https[1]);
				}
			}
		}else{
			define("cityurl",'');
		}
		$AS = implode(" ", $A);
		$LS = implode(" ", $L);
		$exk = explode(',', $cfg_keywords);
		foreach ($exk as $k => $v) {
			$exsk[] = $web.$v;
		}
		$cfg_substation_prefix = $cfg_substation_prefix ? $cfg_substation_prefix : '分站|站群';
        $ex = explode('|', $cfg_substation_prefix);
		foreach ($ex as $k => $v) {
			$exs[] = $web.$v;
		}
		$keywords  = implode(',', $exsk);
        define("keywords",$keywords);
		$title  = implode("|", $exs);
        define("title",$title);
        define("city",$web);
        define("link",$LS);
        define("links",$AS);

        function utf8_str_split($str,$split_len=1){
            if(!preg_match('/^[0-9]+$/', $split_len) || $split_len < 1){
                return FALSE;
            }
            $len = mb_strlen($str, 'UTF-8');
            if ($len <= $split_len){
                return array($str);
            }
            preg_match_all('/.{'.$split_len.'}|[^\x00]{1,'.$split_len.'}$/us', $str, $ar);
            return $ar[0];
        }
?>